package com.wgj.android.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.adapter.MessageAdapter;
import com.wgj.android.entity.Message;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.DateUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/1.
 */

public class MessageActivity extends AppCompatActivity {
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.number1)
    TextView number1;
    @BindView(R.id.number2)
    TextView number2;



    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.type1)
    TextView type1;
    @BindView(R.id.type2)
    TextView type2;


    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.content1)
    TextView content1;
    @BindView(R.id.content2)
    TextView content2;


    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.time1)
    TextView time1;
    @BindView(R.id.time2)
    TextView time2;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_message);
        ButterKnife.bind(this);


        initDate();
    }

    private void initDate() {
       //list = (List<Message>) getIntent().getSerializableExtra("message");
        getMessageType(0, new CallBack() {
            @Override
            public void onSuccess(Message message) {
                content.setText(message.getContent());
                time.setText(DateUtils.dateForm(message.getCreateTime()));
                if(message.getRead()==0){
                    number.setVisibility(View.VISIBLE);
                }else {
                    number.setVisibility(View.GONE);
                }
            }
        });
        getMessageType(1, new CallBack() {
            @Override
            public void onSuccess(Message message) {
                content1.setText(message.getContent());
                time1.setText(DateUtils.dateForm(message.getCreateTime()));
                if(message.getRead()==0){
                    number1.setVisibility(View.VISIBLE);
                }else {
                    number1.setVisibility(View.GONE);
                }
            }
        });
        getMessageType(2, new CallBack() {
            @Override
            public void onSuccess(Message message) {
                content2.setText(message.getContent());
                time2.setText(DateUtils.dateForm(message.getCreateTime()));
                if(message.getRead()==0){
                    number2.setVisibility(View.VISIBLE);
                }else {
                    number2.setVisibility(View.GONE);
                }
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
    @OnClick({R.id.rl,R.id.rl1,R.id.rl2})
    public void doClick(View view){
        Intent intent =new Intent(this,MessageContentActivity.class);
        intent.putExtra("tag",1);
        switch (view.getId()){
            case R.id.rl:
                number.setVisibility(View.GONE);
                intent.putExtra("type",0);
                startActivity(intent);
                break;
            case R.id.rl1:
                number1.setVisibility(View.GONE);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
            case R.id.rl2:
                number2.setVisibility(View.GONE);
                intent.putExtra("type",2);
                startActivity(intent);
                break;

        }

    }
    public void getMessageType(int type , final CallBack callBack){
        XHttp.obtain().get(WGJAPI.MSG_URL + "?mId=" + CacheUtils.getMember().getM_id()+"&typeId="+ type, null, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String string) {
                JSONArray array = null;
                Message message = null;
                try {
                    array = new JSONArray(string);
                    if(array.length()>0){
                        message = new Gson().fromJson(array.getString(0),Message.class);
                        callBack.onSuccess(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("网络异常");
            }
        });
    }
    public interface CallBack{
         void onSuccess(Message message);
    }

}
